<template>
  <div class="content-card">
    <!-- 二级标签 -->
    <!-- 注意官网直接给是on-change事件是不生效的，算是个小坑，加个@就可以了 -->
    <div class="tag-list" v-if="childenTypeList && childenTypeList.length > 0">
      <span>选择分类：</span>
      <Tag
        v-for="(item, index) in childenTypeList"
        :key="index"
        class="tag-item"
        checkable
        :checked="item.isChecked"
        @on-change="changeTag(index)"
        color="success"
        >{{ item.name }}</Tag
      >
      <span @click="selectTag">确定</span>
    </div>
    <div>
      <div v-if="showList" class="ad-content-left">
        <div v-for="(item, index) in list" :key="index">
          <div class="content-box">
            <div class="article-left">
              <a @click="gotoContent(item.id)" class="page-img"
                ><img :src="item.coverImage" alt="内容无法展示..."
              /></a>
            </div>
            <div class="article-right">
              <div class="article-text">
                <div class="article-title">
                  <a @click="gotoContent(item.id)">{{ item.title }}</a>
                </div>
                <div class="list-text">
                  <VueMarkdown
                    class="content-page"
                    :source="item.content"
                  ></VueMarkdown>
                </div>
                <div class="list-meta">
                  <i class="page-top bg-danger">{{ item.tag }}</i>
                  <span class="float-left mr-small">{{ item.autor }}</span
                  >{{ item.times }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-else class="ad-tips content-left ad-content-left">
        该分类下暂无数据，您可以查看其它分类
      </div>
    </div>
  </div>
</template>

<script>
import Home from "./Home";
import VueMarkdown from "vue-markdown";
import Bus from "@/assets/event-bus.js";
import { getArticlesDetail, getClasstypes, getArticleList } from "@/api/common";
export default {
  name: "Article",

  props: [],
  components: { VueMarkdown, Home },
  data() {
    return {
      list: [], //文章列表
      childenTypeList: [],
      adTip: "",
      isBanner: false,
      isAdTipRow: false,
      articleList: [],
      showList: false, //是否显示没有内容的提示
      articleLength: 1, //文章长度
      articleIndex: 0,
      articleObj: [],
      parentId: null //一级分类id
    };
  },
  computed: {},
  watch: {
    $route: {
      //同一个路由，参数不同会触发这里
      handler() {
        if (this.$route.query.type) {
          //点击非首页导航
          this.isBanner = false;
          this.isAdTipRow = false;
          this.adTip = this.$route.query.type;
          this.getArticle(this.$route.query.type);
        } else {
          this.isBanner = true;
          this.isAdTipRow = true;
          this.adTip = "all";
          this.getArticle("all");
        }
      },
      deep: true
    }
  },
  methods: {
    // 选中tag
    changeTag(index) {
      this.childenTypeList[index].isChecked = !this.childenTypeList[index]
        .isChecked;
    },
    // 确认选中tag
    selectTag() {
      let secondIdList = [];
      this.childenTypeList.forEach((item, index1) => {
        if (item.isChecked) {
          secondIdList.push(item.id);
        }
      });

      this.getArticleListL({
        parentId: this.parentId,
        secondIdList: secondIdList
      });
    },

    // 查询文章列表
    getArticleListL(data) {
      return new Promise((resolve, reject) => {
        getArticleList(data).then(
          res => {
            this.list = res.data || [];
            if (this.list && this.list.length > 0) {
              this.showList = true;
            } else {
              this.showList = false;
            }
            resolve(res.data || [])
          },
          error => {
             reject([])
          }
        );
      });
    },

    // 进入文章详情
    gotoContent(id) {
    
      this.$router.push({
        path: "/articleContent",
        query: {
          // vue路由传对象刷新会报错，数据丢失，用json字符串解决
          // articleObj:encodeURIComponent(JSON.stringify(item)),
          id
        }
      });
    }
  },
  async mounted() {

    Bus.$on("getSecondTypes", data => {
      this.childenTypeList = data.children || [];
      this.childenTypeList.forEach((item, index) => {
        // 初始化时不选中
        this.childenTypeList[index].isChecked = false;
      });
      this.parentId = data.id;
      //   查询一级分类下的文章
      this.getArticleListL({
        parentId: this.parentId
      });
    });
  }
};
</script>

<style lang="less" scoped>
// 重置md标签字体大小
/deep/.content-page h1 {
  font-size: 14px !important;
}
/deep/.content-page h2 {
  font-size: 14px !important;
}
.content-card {
  //   background-color: #fff;
  display: inline-block;
  //   height: 155px;
  @media screen and (min-width: 992px) {
    margin: 0 0 30px 0;
    padding: 0 0 30px 0;
    border-bottom: 1px solid #f1f3f5;
  }
  //   二级标签
  .tag-list {
    padding: 5px;
    // background: #ddd;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .tag-item {
      font-size: 14px;
    }
    .tag-item:hover {
      cursor: default;
    }
  }
}
.article-left {
  display: none;
  @media screen and (min-width: 992px) {
    position: relative;
    display: block;
    float: left;
    width: 192px;
    margin-right: -192px;
  }
  .page-img {
    cursor: pointer;
    text-decoration: none;
    color: #212529;
    background-color: transparent;
    display: block;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    img {
      width: 100%;
      height: auto;
      vertical-align: middle;
      border-style: none;
      height: 120px;
    }
  }
}
.article-right {
  width: 100%;
  @media screen and (min-width: 992px) {
    box-sizing: border-box;
    // float: right;
  }
  .article-text {
    display: block;
    @media screen and (min-width: 992px) {
      margin-left: 212px;
    }
    .article-title {
      font-size: 18px;
      font-weight: 700;
      margin-bottom: 10px;
      a {
        cursor: pointer;
        text-decoration: none;
        color: #212529;
        background-color: transparent;
        touch-action: manipulation;
      }
    }
    .list-text {
      margin-bottom: 10px;
      color: #868e96;
      // min-height: 50px;
      max-height: 60px;
      overflow: hidden;
      @media screen and (min-width: 992px) {
        min-height: 50px;
      }
    }
    .list-meta {
      font-size: 12px;
      line-height: 14px;
      color: #868e96;
      .page-top {
        padding: 1px;
        font-size: 12px;
        font-style: normal;
        // line-height: 14px;
        display: block;
        float: left;
        // width: 14px;
        // height: 14px;
        margin-right: 4px;
        text-align: center;
        color: #fff;
        border-radius: 2px;
      }
      .bg-danger {
        background-color: #fa5252 !important;
      }
      .float-left {
        float: left !important;
      }
      .mr-small {
        margin-right: 10px !important;
        height: 16px;
        line-height: 16px;
      }
    }
    // .list-meta::after{
    //     display: block;
    // clear: both;
    // content: "";
    // }
  }
}
.article-right::after {
  display: block;
  clear: both;
  content: "";
}
.ad-content-left {
  float: left;
  width: 100%;
  min-height: 1px;
  padding-right: 10px;
  padding-left: 10px;
  @media screen and (min-width: 992px) {
    width: 640px;
  }
  @media screen and (min-width: 1200px) {
    width: 820px;
  }
  .content-box {
    // background-color: #fff;
    // @media screen and  (min-width: 992px) {
    margin-right: 0;
    margin-bottom: 20px;
    margin-left: 0;
    padding: 30px;
    min-height: 155px;
    background-color: #fff;

    // }
  }
}
.ad-tips {
  font-size: 18px;
  margin-top: 20px;
  text-align: center;
}
</style>
